package sivin.uikit.tags

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import sivin.uikit.utils.dp

class TagContainer @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ViewGroup(context, attrs, defStyleAttr) {

    private var horizontalSpace: Int = 6.0f.dp
    private var verticalSpace: Int = 6.0f.dp
    private var tagShowCount = 0
    private val tagViewList: MutableList<TextView> = mutableListOf()
    private val tagViewHeight: Int = 16.0f.dp
    private val tagViewLeftPadding = 7.0f.dp
    private val tagViewTopPadding = 0


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val containerWidth = MeasureSpec.getSize(widthMeasureSpec)
        var containerHeight = 0
        var maxChildHeightInLine = 0
        var curLineWidth = 0
        val tagWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
        val tagHeightMeasureSpec = MeasureSpec.makeMeasureSpec(tagViewHeight, MeasureSpec.EXACTLY)
        for (i in 0 until tagShowCount) {
            val childView = tagViewList[i]
            childView.measure(tagWidthMeasureSpec, tagHeightMeasureSpec)
            if (maxChildHeightInLine < childView.measuredHeight) {
                maxChildHeightInLine = childView.measuredHeight
                if (containerHeight < maxChildHeightInLine) {
                    containerHeight = maxChildHeightInLine
                }
            }
            if (curLineWidth + childView.measuredWidth + horizontalSpace > containerWidth) {
                containerHeight += maxChildHeightInLine + verticalSpace
                curLineWidth = 0
            } else {
                curLineWidth += childView.measuredWidth + horizontalSpace
            }
        }
        if (containerHeight < minimumHeight) {
            containerHeight = minimumHeight
        }
        setMeasuredDimension(containerWidth, containerHeight)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        var startX = 0
        var startY = 0
        for (i in 0 until tagShowCount) {
            val childView = tagViewList[i]
            if (startX + childView.measuredWidth + horizontalSpace > measuredWidth) {
                startX = 0
                startY = childView.measuredHeight + verticalSpace
            }
            childView.layout(
                startX,
                startY,
                startX + childView.measuredWidth,
                startY + childView.measuredHeight
            )
            startX += childView.measuredWidth + horizontalSpace
        }
    }

    fun setHorizontalSpaceDp(spec: Float) {
        horizontalSpace = spec.dp
    }

    fun setData(dataList: List<String>) {
        if (dataList.size > tagViewList.size) {
            val diff = dataList.size - tagViewList.size
            for (i in 0..diff) {
                val tagView = createTagView(context)
                tagViewList.add(tagView)
                addView(tagView)
            }
        } else if (dataList.size < tagViewList.size) {
            val diff = tagViewList.size - dataList.size
            val startIndex = tagViewList.size - diff
            for (i in startIndex until tagViewList.size) {
                tagViewList[i].visibility = GONE
            }
        }

        tagShowCount = dataList.size
        for (i in dataList.indices) {
            tagViewList[i].visibility = VISIBLE
            tagViewList[i].text = dataList[i]
        }
    }

    private fun createTagView(context: Context): TextView {
        val textView = TextView(context)
        textView.textSize = 10f
        textView.gravity = Gravity.CENTER
        textView.setPadding(
            tagViewLeftPadding,
            tagViewTopPadding,
            tagViewLeftPadding,
            tagViewTopPadding
        )
        textView.setTextColor(Color.parseColor("#ff00C584"))
        return textView
    }
}
